package com.sau.pixelcart.controller;


import com.sau.pixelcart.common.result.PageResult;
import com.sau.pixelcart.common.result.Result;
import com.sau.pixelcart.domain.dto.OrderDTO;
import com.sau.pixelcart.domain.dto.OrderPageDTO;
import com.sau.pixelcart.domain.dto.SalesAnalysisDTO;
import com.sau.pixelcart.domain.vo.OrderVO;
import com.sau.pixelcart.domain.vo.SalesAnalysisVO;
import com.sau.pixelcart.service.IOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 订单核心信息（日均百万级数据需考虑分表） 前端控制器
 * </p>
 *
 * @author Inton
 * @since 2025-03-27
 */
@Slf4j
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    /**
     * 订单分页查询
     * @param orderPageDTO
     * @return
     */
    @PostMapping("/list")
    public Result list(@RequestBody OrderPageDTO orderPageDTO) {
        log.info("订单分页查询：{}", orderPageDTO);
        PageResult<OrderVO> pageResult = ordersService.pageQuery(orderPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 创建订单
     * @param orderDTO
     * @return
     */
    @PostMapping("/saveBatch")
    public Result save(@RequestBody OrderDTO orderDTO){
        log.info("生成订单：{}", orderDTO);
        ordersService.saveOrder(orderDTO);
        return Result.success();
    }

    @PostMapping("/sales/analysis")
    public Result saleAnalysis(@RequestBody SalesAnalysisDTO salesAnalysisDTO){
        log.info("开始销售分析：{}", salesAnalysisDTO);
        SalesAnalysisVO salesAnalysisVO = ordersService.saleAnalysis(salesAnalysisDTO);
        return Result.success(salesAnalysisVO);
    }

    /**
     * 导出订单报表
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        log.info("导出订单报表");
        ordersService.exportOrders(response);
    }

    /**
     * 处理待支付订单
     * @param orderDTO
     */
    @PostMapping("/solveOrder")
    public Result solveNoPayOrder(@RequestBody OrderDTO orderDTO){
        log.info("处理待支付订单");
        ordersService.solveNoPayOrder(orderDTO);
        return Result.success();
    }
}
